package model

import (
	"github.com/jinzhu/gorm"
	"go-member/db"
)

// User
// @Description: 查询结构体
type User struct {
	Id         uint
	Name       string
	Phone      string
	Money      string
	Number     uint
	Created_at string
	Updated_at string
	Deleted_at string
}

// AddUser
// @Description: 添加结构体
type AddUser struct {
	Id         uint
	Name       string
	Phone      string
	Money      string
	Number     uint
	Created_at string
	Updated_at string
}

type UserStruct struct {
	List        interface{}
	Total       uint
	CurrentPage uint
}

func (User) TableName() string {
	return "user"
}

// ExistsUserName
// @Description: 检查名字是否存在
// @param val
// @return bool
func ExistsUserName(val string) bool {
	mysql := db.InitMysql()
	user := User{}
	e := mysql.Where("name= ?", val).Debug().First(&user).Error
	if e == gorm.ErrRecordNotFound {
		return false
	}
	return true
}

// ExistsUser
// @Description:检测用户是否存在
// @param val
// @return bool
func ExistsUser(val string) bool {
	mysql := db.InitMysql()
	user := User{}
	err := mysql.Where("id=?", val).Debug().First(&user).Error
	if err == gorm.ErrRecordNotFound {
		return false
	}
	return true
}

// ExistsDelUser
// @Description:检测回收站用户是否存在
// @param val
// @return bool
func ExistsDelUser(val string) bool {
	mysql := db.InitMysql()
	user := User{}
	err := mysql.Raw("SELECT * FROM `user`  WHERE (id=?) AND (deleted_at IS NOT NULL)", val).Debug().Scan(&user).Error
	if err == gorm.ErrRecordNotFound {
		return false
	}
	return true
}

// GetUserById
// @Description: 通过id获取用户
// @param id
// @return any
func GetUserById(id any) (User, bool) {
	mysql := db.InitMysql()
	user := User{}
	e := mysql.Where("id= ?", id).Debug().First(&user).Error
	if e == gorm.ErrRecordNotFound {
		return user, false
	}
	return user, true
}

// ExistsUserNameAssignById
// @Description: 查询
// @param id
// @param name
// @return bool
func ExistsUserNameAssignById(id int, name string) bool {
	mysql := db.InitMysql()
	user := User{}
	e := mysql.Debug().Where("id!= ?", id).Where("name=?", name).First(&user).Error
	if e == gorm.ErrRecordNotFound {
		return false
	}
	return true
}
